//
//  MILForgetPasswordViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/12.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILForgetPasswordViewController.h"
#import "GetRestCodeApi.h"
#import "RestPwdApi.h"

@interface MILForgetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myUserNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *myCoderTextField;
@property (weak, nonatomic) IBOutlet UITextField *myNewPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *meGetCoderBtn;
@property(nonatomic,assign)NSInteger temptime;//获得验证码后时间倒数
@property (nonatomic , strong) NSTimer *TimerWithTimerout;
@end

@implementation MILForgetPasswordViewController
#pragma mark -- UI
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"忘记密码"];
    self.navigationBar.backgroundColor = [UIColor blackColor];
    [self setLeftButtonImage:[UIImage imageNamed:@"back_icon"]];
    self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
}

#pragma mark -- 按钮点击
//获取验证码
- (IBAction)getForgetPasswordCoderAction:(UIButton *)sender {
    if (!self.myUserNameTextField.text || [self.myUserNameTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空！"];
        return;
    }
    
    if (![self.myUserNameTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    //获取验证码
    //调用获取验证码接口
    GetRestCodeApi *api = [[GetRestCodeApi alloc]initWithDic:@{@"username":self.myUserNameTextField.text,@"type":@(1)}];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary * dict = [api responseJSONObject];
        NSLog(@"dict = %@",dict[@"errmsg"]);
        if ([dict[@"errcode"] intValue] == 0) {
            
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];
            self.temptime = 60;
            [self.meGetCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [self.meGetCoderBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.temptime] forState:UIControlStateNormal];
            self.meGetCoderBtn.enabled = NO;
            [self.TimerWithTimerout invalidate];
            self.TimerWithTimerout = nil;
            self.TimerWithTimerout = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                      target:self selector:@selector(VersionTimerout)
                                                                    userInfo:nil
                                                                     repeats:YES];
            
        } else {
            [SVProgressHUD showErrorWithStatus:dict[@"errmsg"]];
            [self.TimerWithTimerout invalidate];
            self.TimerWithTimerout = nil;
            self.meGetCoderBtn.enabled = YES;
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
        [self.TimerWithTimerout invalidate];
        self.TimerWithTimerout = nil;
        NSString *dic=[request.error description];
        NSLog(@"YTKBaseRequest====%@",dic);
        self.meGetCoderBtn.enabled = YES;
    }];
    

}

//确定
- (IBAction)sureBtnClickAction:(UIButton *)sender {
    
    //调用注册的接口
    if (!self.myUserNameTextField.text || [self.myUserNameTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空！"];
        return;
    }
    if (!self.myCoderTextField.text || [self.myCoderTextField.text isEqualToString:@""] ) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空！"];
        return;
    }
    if (!self.myNewPasswordTextField.text || [self.myNewPasswordTextField.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空！"];
        return;
    }
    if (self.myNewPasswordTextField.text.length>16 || self.myNewPasswordTextField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"密码长度应该在6-16位之间"];
        return;
    }
    
    if (![self.myUserNameTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![self.myNewPasswordTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-16位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    [self confirmActionStart];
    

    

}

// 开始确定
- (void)confirmActionStart {
    RestPwdApi *restPasswordApi = [[RestPwdApi alloc] initWithUsername:self.myUserNameTextField.text password:self.myNewPasswordTextField.text code:self.myCoderTextField.text];
    [restPasswordApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary * dic = request.responseJSONObject;
        if ([dic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.updateBlock) {
                    self.updateBlock(self.myUserNameTextField.text);
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        [SVProgressHUD showErrorWithStatus:@"重置密码失败"];
    }];
}

//点击后时间倒数
- (void)VersionTimerout
{
    if (self.temptime == 0)
    {
        [self.TimerWithTimerout invalidate];
        self.TimerWithTimerout = nil;
        [self.meGetCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.meGetCoderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self.meGetCoderBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.meGetCoderBtn.enabled = YES;
        //        [self.sendCodeBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
        return;
    }
    self.temptime--;
    [self.meGetCoderBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.meGetCoderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.meGetCoderBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.temptime] forState:UIControlStateNormal];
    self.meGetCoderBtn.enabled = NO;
}


@end
